﻿/**
 * @file		ct_dd_pmc_calendar.c
 * @brief		PMC Calendar driver Component Test
 * @note		None
 * @attention	None
 * 
 * <B><I>Copyright 2016 Socionext Inc.</I></B>
 */

#include <stdlib.h>
#include <string.h>
#include "driver_common.h"
#include "dd_pmc_calendar.h"
#include "dd_pmc_exiu.h"
#include "pmc.h"

/*----------------------------------------------------------------------*/
/* Definition															*/
/*----------------------------------------------------------------------*/
// Nothing Special

/*----------------------------------------------------------------------*/
/* Enumeration															*/
/*----------------------------------------------------------------------*/
// Nothing Special

/*----------------------------------------------------------------------*/
/* Structure															*/
/*----------------------------------------------------------------------*/
// Nothing Special

/*----------------------------------------------------------------------*/
/* Global Data															*/
/*----------------------------------------------------------------------*/
// Nothing Special

/*----------------------------------------------------------------------*/
/* Local Method Definition												*/
/*----------------------------------------------------------------------*/
// Nothing Special

/*----------------------------------------------------------------------*/
/* Local Function														*/
/*----------------------------------------------------------------------*/
// Nothing Special

/*----------------------------------------------------------------------*/
/* Global Function														*/
/*----------------------------------------------------------------------*/
/**
 * @brief  Command main function for Calendar test.
 * @param  int argc	:The number of parameters
 *		   char** argv	:The value of parameters
 * @return VOID
 *
 *		The meaning of parameters
 *		+-----------+-------+-------+---------------------------------------------+
 *		| P1        | P2    | P3    |  Meaning                                    |
 *		+-----------+-------+-------+---------------------------------------------+
 *		| init      |               | initialization                              |
 *		+-----------+---------------+---------------------------------------------+
 *		| reset     |               | Software reset                              |
 *		+-----------+-------+-------+---------------------------------------------+
 *		| set       | 0     |       | Set Calendar (pattern 1)                    |
 *		|           +-------+-------+---------------------------------------------+
 *		|           | 1     |       | Set Calendar (pattern 2)                    |
 *		|           +-------+-------+---------------------------------------------+
 *		|           | 2     |       | Set Calendar (Invalid parameter)            |
 *		|           +-------+-------+---------------------------------------------+
 *		|           | 3     |       | Set Calendar (Null parameter)               |
 *		+-----------+-------+-------+---------------------------------------------+
 *		| get       | 0     |       | Get Calendar                                |
 *		|           +-------+-------+---------------------------------------------+
 *		|           | 1     |       | Get Calendar (Null parameter)               |
 *		+-----------+-------+-------+---------------------------------------------+
 *		| alarm     | set   | 0     | Set Alarm (pattern 1)                       |
 *		|           |       +-------+---------------------------------------------+
 *		|           |       | 1     | Set Alarm (pattern 2)                       |
 *		|           |       +-------+---------------------------------------------+
 *		|           |       | 2     | Set Alarm (Invalid parameter)               |
 *		|           |       +-------+---------------------------------------------+
 *		|           |       | 3     | Set Alarm (Null parameter)                  |
 *		|           +-------+-------+---------------------------------------------+
 *		|           | get   | 0     | Get Alarm                                   |
 *		|           |       +-------+---------------------------------------------+
 *		|           |       | 1     | Get Alarm (Null parameter)                  |
 *		+-----------+-------+-------+---------------------------------------------+
 *		| 1hz       | 0/1   |       | Set 1Hz output enable (0:disable, 1:enable) |
 *		|           +-------+-------+---------------------------------------------+
 *		|           | other |       | Set 1Hz output enable (Invalid parameter)   |
 *		+-----------+-------+-------+---------------------------------------------+
 *		| test      | set   | 1-8   | Set Test mode                               |
 *		|           |       +-------+---------------------------------------------+
 *		|           |       | other | Set Test mode (Invalid parameter)           |
 *		|           +-------+-------+---------------------------------------------+
 *		|           | get   | 0     | Get Test mode                               |
 *		|           |       +-------+---------------------------------------------+
 *		|           |       | 1     | Get Test mode (Null parameter)              |
 *		+-----------+-------+-------+---------------------------------------------+
 */
VOID Ct_Dd_Pmc_Calendar_Main(int argc, char** argv)
{
	INT32						ret;
	int							param2;
	int							param3;
	T_DD_PMC_CALENDAR_DATA		calendar;
	T_DD_PMC_CALENDAR_ALARM		alarm;
	E_DD_PMC_CALENDAR_TEST_MODE	test_mode;
	
	if(strcmp(argv[1], "init") == 0){
		// Initialization
		Dd_PMC_Calendar_Init();
		Ddim_Print(("Dd_PMC_Calendar_Init : execute.\n"));
		// register data output.
		Ddim_Print(("--- CALENDAR Register ---\n"));
		Ddim_Print(("CAC : 0x%08x\n", IO_PMC.CALENDAR.CAC.word));
		Ddim_Print(("CA1 : 0x%08x\n", IO_PMC.CALENDAR.CA1.word));
		Ddim_Print(("CA2 : 0x%08x\n", IO_PMC.CALENDAR.CA2.word));
		Ddim_Print(("CA3 : 0x%08x\n", IO_PMC.CALENDAR.CA3.word));
		Ddim_Print(("CA4 : 0x%08x\n", IO_PMC.CALENDAR.CA4.word));
		Ddim_Print(("CA5 : 0x%08x\n", IO_PMC.CALENDAR.CA5.word));
		Ddim_Print(("CA6 : 0x%08x\n", IO_PMC.CALENDAR.CA6.word));
		Ddim_Print(("CA7 : 0x%08x\n", IO_PMC.CALENDAR.CA7.word));
		Ddim_Print(("-------------------------\n"));
	}
	else if(strcmp(argv[1], "reset") == 0){
		// Software reset.
		Dd_PMC_Calendar_SR();
		Ddim_Print(("Dd_PMC_Calendar_SR : execute.\n"));
		// register data output.
		Ddim_Print(("--- CALENDAR Register ---\n"));
		Ddim_Print(("CAC : 0x%08x\n", IO_PMC.CALENDAR.CAC.word));
		Ddim_Print(("-------------------------\n"));
	}
	else if(strcmp(argv[1], "set") == 0){
		// get 2nd paramter.
		param2 = atoi(argv[2]);
		if (param2 == 0) {
			// set calendar data.
			calendar.second			= 0;
			calendar.minute			= 0;
			calendar.hour			= 0;
			calendar.day			= 1;
			calendar.day_of_week	= D_DD_PMC_CALENDAR_DAY_OF_WEEK_SUN;
			calendar.month			= 1;
			calendar.year			= 0;
			// set Calendar.
			ret = Dd_PMC_Calendar_Set_Calendar(&calendar);
			if (ret == D_DDIM_OK) {
				Ddim_Print(("Dd_PMC_Calendar_Set_Calendar : OK\n"));
				// register data output.
				Ddim_Print(("--- CALENDAR Register ---\n"));
				Ddim_Print(("CAC : 0x%08x\n", IO_PMC.CALENDAR.CAC.word));
				Ddim_Print(("CA1 : 0x%08x\n", IO_PMC.CALENDAR.CA1.word));
				Ddim_Print(("CA2 : 0x%08x\n", IO_PMC.CALENDAR.CA2.word));
				Ddim_Print(("CA3 : 0x%08x\n", IO_PMC.CALENDAR.CA3.word));
				Ddim_Print(("CA4 : 0x%08x\n", IO_PMC.CALENDAR.CA4.word));
				Ddim_Print(("CA5 : 0x%08x\n", IO_PMC.CALENDAR.CA5.word));
				Ddim_Print(("CA6 : 0x%08x\n", IO_PMC.CALENDAR.CA6.word));
				Ddim_Print(("CA7 : 0x%08x\n", IO_PMC.CALENDAR.CA7.word));
				Ddim_Print(("-------------------------\n"));
			}
			else {
				Ddim_Print(("Dd_PMC_Calendar_Set_Calendar : NG  ret=0x%08x\n", ret));
			}
		}
		else if (param2 == 1) {
			// set calendar data.
			calendar.second			= 59;
			calendar.minute			= 59;
			calendar.hour			= 23;
			calendar.day			= 31;
			calendar.day_of_week	= D_DD_PMC_CALENDAR_DAY_OF_WEEK_SAT;
			calendar.month			= 12;
			calendar.year			= 99;
			// set Calendar.
			ret = Dd_PMC_Calendar_Set_Calendar(&calendar);
			if (ret == D_DDIM_OK) {
				Ddim_Print(("Dd_PMC_Calendar_Set_Calendar : OK\n"));
				// register data output.
				Ddim_Print(("--- CALENDAR Register ---\n"));
				Ddim_Print(("CAC : 0x%08x\n", IO_PMC.CALENDAR.CAC.word));
				Ddim_Print(("CA1 : 0x%08x\n", IO_PMC.CALENDAR.CA1.word));
				Ddim_Print(("CA2 : 0x%08x\n", IO_PMC.CALENDAR.CA2.word));
				Ddim_Print(("CA3 : 0x%08x\n", IO_PMC.CALENDAR.CA3.word));
				Ddim_Print(("CA4 : 0x%08x\n", IO_PMC.CALENDAR.CA4.word));
				Ddim_Print(("CA5 : 0x%08x\n", IO_PMC.CALENDAR.CA5.word));
				Ddim_Print(("CA6 : 0x%08x\n", IO_PMC.CALENDAR.CA6.word));
				Ddim_Print(("CA7 : 0x%08x\n", IO_PMC.CALENDAR.CA7.word));
				Ddim_Print(("-------------------------\n"));
			}
			else {
				Ddim_Print(("Dd_PMC_Calendar_Set_Calendar : NG  ret=0x%08x\n", ret));
			}
		}
		else if (param2 == 2) {
			// set calendar data.
			calendar.second			= 60;	// max second value over.
			calendar.minute			= 59;
			calendar.hour			= 23;
			calendar.day			= 31;
			calendar.day_of_week	= D_DD_PMC_CALENDAR_DAY_OF_WEEK_SAT;
			calendar.month			= 12;
			calendar.year			= 99;
			// set Calendar.
			ret = Dd_PMC_Calendar_Set_Calendar(&calendar);
			Ddim_Print(("Dd_PMC_Calendar_Set_Calendar : NG  ret=0x%08x\n", ret));
		}
		else if (param2 == 3) {
			// input paramter error.
			ret = Dd_PMC_Calendar_Set_Calendar(NULL);
			Ddim_Print(("Dd_PMC_Calendar_Set_Calendar : NG  ret=0x%08x\n", ret));
		}
		else {
			Ddim_Print(("please check 2nd parameter!!\n"));
		}
	}
	else if(strcmp(argv[1], "get") == 0){
		// get 2nd paramter.
		param2 = atoi(argv[2]);
		if (param2 == 0) {
			// calendar data clear.
			memset(&calendar, 0, sizeof(T_DD_PMC_CALENDAR_DATA));
			// get calendar.
			ret = Dd_PMC_Calendar_Get_Calendar(&calendar);
			if (ret == D_DDIM_OK) {
				Ddim_Print(("Dd_PMC_ADC_Get_Ctrl : OK\n"));
				// register data output.
				Ddim_Print(("--- CALENDAR Register ---\n"));
				IO_PMC.CALENDAR.CAC.word	= 0x01;
				Ddim_Print(("CAC : 0x%08x\n", IO_PMC.CALENDAR.CAC.word));
				Ddim_Print(("CA1 : 0x%08x\n", IO_PMC.CALENDAR.CA1.word));
				Ddim_Print(("CA2 : 0x%08x\n", IO_PMC.CALENDAR.CA2.word));
				Ddim_Print(("CA3 : 0x%08x\n", IO_PMC.CALENDAR.CA3.word));
				Ddim_Print(("CA4 : 0x%08x\n", IO_PMC.CALENDAR.CA4.word));
				Ddim_Print(("CA5 : 0x%08x\n", IO_PMC.CALENDAR.CA5.word));
				Ddim_Print(("CA6 : 0x%08x\n", IO_PMC.CALENDAR.CA6.word));
				Ddim_Print(("CA7 : 0x%08x\n", IO_PMC.CALENDAR.CA7.word));
				IO_PMC.CALENDAR.CAC.word	= 0x00;
				// get data output.
				Ddim_Print(("--- Get Data -------\n"));
				Ddim_Print(("year        : %d\n", calendar.year));
				Ddim_Print(("month       : %d\n", calendar.month));
				Ddim_Print(("day         : %d\n", calendar.day));
				Ddim_Print(("day_of_week : %d\n", calendar.day_of_week));
				Ddim_Print(("hour        : %d\n", calendar.hour));
				Ddim_Print(("minute      : %d\n", calendar.minute));
				Ddim_Print(("second      : %d\n", calendar.second));
				Ddim_Print(("-------------------------\n"));
			}
			else {
				Ddim_Print(("Dd_PMC_Calendar_Get_Calendar : NG  ret=0x%08x\n", ret));
			}
		}
		else if (param2 == 1) {
			// input paramter error.
			ret = Dd_PMC_Calendar_Get_Calendar(NULL);
			Ddim_Print(("Dd_PMC_Calendar_Get_Calendar : NG  ret=0x%08x\n", ret));
		}
		else {
			Ddim_Print(("please check 2nd parameter!!\n"));
		}
	}
	else if(strcmp(argv[1], "alarm") == 0){
		if(strcmp(argv[2], "set") == 0){
			// get 3rd paramter.
			param3 = atoi(argv[3]);
			if (param3 == 0) {
				alarm.minute		= 0;
				alarm.hour			= 0;
				alarm.day			= 1;
				alarm.day_of_week	= D_DD_PMC_CALENDAR_DAY_OF_WEEK_SUN;
				ret = Dd_PMC_Calendar_Set_Alarm(&alarm);
				if (ret == D_DDIM_OK) {
					Ddim_Print(("Dd_PMC_Calendar_Set_Alarm : OK\n"));
					// register data output.
					Ddim_Print(("--- CALENDAR Register ---\n"));
					Ddim_Print(("CAC : 0x%08x\n", IO_PMC.CALENDAR.CAC.word));
					Ddim_Print(("AL1 : 0x%08x\n", IO_PMC.CALENDAR.AL1.word));
					Ddim_Print(("AL2 : 0x%08x\n", IO_PMC.CALENDAR.AL2.word));
					Ddim_Print(("AL3 : 0x%08x\n", IO_PMC.CALENDAR.AL3.word));
					Ddim_Print(("AL4 : 0x%08x\n", IO_PMC.CALENDAR.AL4.word));
					Ddim_Print(("-------------------------\n"));
				}
				else {
					Ddim_Print(("Dd_PMC_Calendar_Set_Alarm : NG  ret=0x%08x\n", ret));
				}
			}
			else if(param3 == 1){
				alarm.minute		= 59;
				alarm.hour			= 23;
				alarm.day			= 31;
				alarm.day_of_week	= D_DD_PMC_CALENDAR_DAY_OF_WEEK_SUN;
				ret = Dd_PMC_Calendar_Set_Alarm(&alarm);
				if (ret == D_DDIM_OK) {
					Ddim_Print(("Dd_PMC_Calendar_Set_Alarm : OK\n"));
					// register data output.
					Ddim_Print(("--- CALENDAR Register ---\n"));
					Ddim_Print(("CAC : 0x%08x\n", IO_PMC.CALENDAR.CAC.word));
					Ddim_Print(("AL1 : 0x%08x\n", IO_PMC.CALENDAR.AL1.word));
					Ddim_Print(("AL2 : 0x%08x\n", IO_PMC.CALENDAR.AL2.word));
					Ddim_Print(("AL3 : 0x%08x\n", IO_PMC.CALENDAR.AL3.word));
					Ddim_Print(("AL4 : 0x%08x\n", IO_PMC.CALENDAR.AL4.word));
					Ddim_Print(("-------------------------\n"));
				}
				else {
					Ddim_Print(("Dd_PMC_Calendar_Set_Alarm : NG  ret=0x%08x\n", ret));
				}
			}
			else if(param3 == 2){
				alarm.minute		= 59;
				alarm.hour			= 24;		// max hour value over.
				alarm.day			= 31;
				alarm.day_of_week	= D_DD_PMC_CALENDAR_DAY_OF_WEEK_SUN;
				ret = Dd_PMC_Calendar_Set_Alarm(&alarm);
				Ddim_Print(("Dd_PMC_Calendar_Set_Alarm : NG  ret=0x%08x\n", ret));
			}
			else if(param3 == 3){
				// input paramter error.
				ret = Dd_PMC_Calendar_Set_Alarm(NULL);
				Ddim_Print(("Dd_PMC_Calendar_Set_Alarm : NG  ret=0x%08x\n", ret));
			}
			else {
				Ddim_Print(("please check 3rd parameter!!\n"));
			}
		}
		else if(strcmp(argv[2], "get") == 0){
			// get 3rd paramter.
			param3 = atoi(argv[3]);
			if (param3 == 0) {
				// alarm data clear.
				memset(&alarm, 0, sizeof(T_DD_PMC_CALENDAR_ALARM));
				ret = Dd_PMC_Calendar_Get_Alarm(&alarm);
				if (ret == D_DDIM_OK) {
					Ddim_Print(("Dd_PMC_Calendar_Get_Alarm : OK\n"));
					// register data output.
					Ddim_Print(("--- CALENDAR Register ---\n"));
					Ddim_Print(("CAC : 0x%08x\n", IO_PMC.CALENDAR.CAC.word));
					Ddim_Print(("AL1 : 0x%08x\n", IO_PMC.CALENDAR.AL1.word));
					Ddim_Print(("AL2 : 0x%08x\n", IO_PMC.CALENDAR.AL2.word));
					Ddim_Print(("AL3 : 0x%08x\n", IO_PMC.CALENDAR.AL3.word));
					Ddim_Print(("AL4 : 0x%08x\n", IO_PMC.CALENDAR.AL4.word));
					// get data output.
					Ddim_Print(("--- Get Data -------\n"));
					Ddim_Print(("day         : %d\n", alarm.day));
					Ddim_Print(("day_of_week : %d\n", alarm.day_of_week));
					Ddim_Print(("hour        : %d\n", alarm.hour));
					Ddim_Print(("minute      : %d\n", alarm.minute));
					Ddim_Print(("-------------------------\n"));
				}
				else {
					Ddim_Print(("Dd_PMC_Calendar_Get_Alarm : NG  ret=0x%08x\n", ret));
				}
			}
			else if(param3 == 1){
				// input paramter error.
				ret = Dd_PMC_Calendar_Get_Alarm(NULL);
				Ddim_Print(("Dd_PMC_Calendar_Get_Alarm : NG  ret=0x%08x\n", ret));
			}
			else {
				Ddim_Print(("please check 3rd parameter!!\n"));
			}
		}
		else {
			Ddim_Print(("please check 2nd parameter!!\n"));
		}
	}
	else if(strcmp(argv[1], "1hz") == 0){
		// get 2nd paramter.
		param2 = atoi(argv[2]);
		ret = Dd_PMC_Calendar_Set_1Hz_Output_Enable(param2);
		if (ret == D_DDIM_OK) {
			Ddim_Print(("Dd_PMC_Calendar_Set_1Hz_Output_Enable : OK\n"));
			// register data output.
			Ddim_Print(("--- CALENDAR Register ---\n"));
			Ddim_Print(("CAC : 0x%08x\n", IO_PMC.CALENDAR.CAC.word));
			Ddim_Print(("-------------------------\n"));
		}
		else {
			Ddim_Print(("Dd_PMC_Calendar_Set_1Hz_Output_Enable : NG  ret=0x%08x\n", ret));
		}
	}
	else if(strcmp(argv[1], "test") == 0){
		if(strcmp(argv[2], "set") == 0){
			// get 3rd paramter.
			param3 = atoi(argv[3]);
			if ((param3 >= E_DD_PMC_CALENDAR_TEST_MODE_15STEP_DIVIDER) &&
				(param3 <= E_DD_PMC_CALENDAR_TEST_MODE_RESET)){
				// Set Test Mode.
				Dd_PMC_Calendar_Set_Test_Mode((E_DD_PMC_CALENDAR_TEST_MODE)param3);
				Ddim_Print(("Dd_PMC_Calendar_Set_Test_Mode : execute.\n"));
				// register data output.
				Ddim_Print(("--- CALENDAR Register ---\n"));
				Ddim_Print(("TST : 0x%08x\n", IO_PMC.CALENDAR.TST.word));
				Ddim_Print(("-------------------------\n"));
			}
			else {
				Ddim_Print(("please check 3rd parameter!!\n"));
			}
		}
		else if(strcmp(argv[2], "get") == 0){
			// get 3rd paramter.
			param3 = atoi(argv[3]);
			if (param3 == 0) {
				// Get Test Mode.
				ret = Dd_PMC_Calendar_Get_Test_Mode(&test_mode);
				if (ret == D_DDIM_OK) {
					Ddim_Print(("Dd_PMC_Calendar_Get_Test_Mode : OK\n"));
					// register data output.
					Ddim_Print(("--- CALENDAR Register ---\n"));
					Ddim_Print(("TST : 0x%08x\n", IO_PMC.CALENDAR.TST.word));
					// get data output.
					Ddim_Print(("--- Get Data -------\n"));
					Ddim_Print(("test_mode = %d\n", test_mode));
					Ddim_Print(("-------------------------\n"));
				}
				else {
					Ddim_Print(("Dd_PMC_Calendar_Get_Test_Mode : NG  ret=0x%08x\n", ret));
				}
			}
			else if (param3 == 1) {
				// input paramter error.
				ret = Dd_PMC_Calendar_Get_Test_Mode(NULL);
				Ddim_Print(("Dd_PMC_Calendar_Get_Test_Mode : NG  ret=0x%08x\n", ret));
			}
		}
		else {
			Ddim_Print(("please check 2nd parameter!!\n"));
		}
	}
	else if(strcmp(argv[1], "alarm_int") == 0){
		// Init
		Dd_PMC_Calendar_Init();
		
		// Set EXINT16.
		Dd_PMC_EXIU_Set_EILVL_Lo(16);			// Low level.
		Dd_PMC_EXIU_Set_EIREQCLR_Ch(16);		// Clear Interrupt Request.
		Dd_PMC_EXIU_Set_EIMASK_Release(16);		// Mask Release.
			
		// Calendar Alarm.
		// Set Calendar : 2016/06/10 Fri 21:30:30
		calendar.second			= 30;
		calendar.minute			= 30;
		calendar.hour			= 21;
		calendar.day			= 10;
		calendar.day_of_week	= D_DD_PMC_CALENDAR_DAY_OF_WEEK_FRI;
		calendar.month			= 6;
		calendar.year			= 16;
		Dd_PMC_Calendar_Set_Calendar(&calendar);
		
		// Set Alarm : 2016/06/10 Fri 21:31:00
		alarm.minute			= 31;
		alarm.hour				= 21;
		alarm.day				= 10;
		alarm.day_of_week		= D_DD_PMC_CALENDAR_DAY_OF_WEEK_FRI;
		Dd_PMC_Calendar_Set_Alarm(&alarm);
		
		// Interrupt occurs after the lapse of 30 seconds.
	}
	else{
		Ddim_Print(("please check 1st parameter!!\n"));
	}
}
